/* Copyright 2002 -- Ira R. Forman and Nate Forman */ 
/**
 * The Args class provides (static) utility methods for processing
 * a command-line argument array.  The usual conventions are assumed.
 * That is, flags preceed positional arguments and a flag is string
 * that begins with a hyphen. A flag is permitted to be followed by a
 * mandatory parameter.
 */
//start extract Args
package c2c;
import java.util.Vector;
import mopex.UQueue;

public class Args {

//stop extract Args
/**
 * Argument processing begins with a call to Args.start, which
 * store the argurment array in the static field args.
 */
//start extract Args
    private String[] args;
//stop extract Args
/**
 * This array is used to record which arguments have been processed
 * (at least once). 
 */
//start extract Args
    private boolean[] argProcessed;

//stop extract Args
/**
 * Begins the argument processing by storing the argument arrays
 * and initializing argProcessed array.  By default, each element of argProcessed
 * is intialized to false.
 */
//start extract Args
    Args( String[] cmdArgs ) {
	args = cmdArgs;
	argProcessed = new boolean[ args.length ];
    }

//stop extract Args
/**
 * Processes the last argument, which should be the 
 * input class name.
 */
//start extract Args
    public String getLast() {
	if ( args[args.length-1].charAt(0) == '-' )
	    return null;
	String returnValue = args[args.length-1];
	argProcessed[args.length-1] = true;
	return returnValue;
    }

//stop extract Args
/**
 * Returns the mandatory parameter string that follows the flag.
 * The flag must begin with hyphen.  Once the flag is found and the
 * parameter is removed from the args, both the flag and the parameter
 * entries in the args array are set to null.
 */
//start extract Args
    public String getFlagValue( String flag ) {
	for ( int i = 0; i < args.length-1; i++ )
	    if ( !argProcessed[i] && !argProcessed[i+1] 
		                  && args[i].equals(flag) 
		                  && args[i].charAt(0) == '-' 
		                  && args[i+1].charAt(0) != '-' ) {
		String returnValue = args[i+1];
		argProcessed[i] = true;
		argProcessed[i+1] = true;
		return returnValue;
	    }
	return null;
    }

//stop extract Args
/**
 *  If a flag is permitted to be repeated in a command line, getFlagValues returns a Vector
 *  of all the mandatory parameters that follow the matching flag. All matching flags and
 *  corresponding parameters in args are set to null after processing.
 */
//start extract Args
    public UQueue getFlagValues( String flag ){
	UQueue  values = new UQueue( String.class );
	String value = getFlagValue( flag );
	while ( value != null ) {
	    values.add(value);
	    value = getFlagValue( flag );
	}
	return values;
    }

//stop extract Args
/**
 *  Returns true if the flag appears in the command line (args).
 *  If found, sets the args entry of the flag to null.
 */
//start extract Args
    public boolean hasFlag( String flag ) {
	for ( int i = 0; i < args.length; i++ )
	    if ( args[i] != null && args[i].equals(flag) 
		                 && args[i].charAt(0) == '-' ) {
		argProcessed[i] = true;
		return true;
	    }
	return false;
    }

//stop extract Args
/**
 *  Returns true if the all entries of the args array are null.
 */
//start extract Args
    public boolean complete(){
	for ( int i = 0; i < argProcessed.length; i++ )
	    if ( !argProcessed[i] )
		return false;
	return true;
    }

//stop extract Args
/**
 *  Converts all non-processed entries of args into a single string.
 */
//start extract Args
    public String toString(){
	String result = "";
	for ( int i = 0; i < args.length; i++ )
	    if ( !argProcessed[i] )
		result += args[i] + " ";
	return result;
    }
}
//stop extract Args
